/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-15 11:57:11
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-28 14:47:26
 * @FilePath: \cc-game-development-template\assets\game.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-15 11:57:11
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-17 09:04:56
 * @FilePath: \cc-game-development-template\assets\scripts\game.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Animation, _decorator, Button, Color, Component, EventTouch, find, Label, macro, math, Node, NodeEventType, Prefab, Quat, resources, Sprite, SpriteFrame, Tween, tween, UITransform, v2, Vec2, Vec3, sys, view, instantiate, random } from 'cc';
import { Constant, EventConst } from '../../../common/Common';
import { App } from '../../../App';
import { LoadMg } from '../../../tools/LoadMg';
import UtilTool from '../../../tools/UtilTool';
import AbstractComponent from '../../../base/AbstractComponent';
import BilliardsGameController from '.././controller/BilliardsGameController';
import { GameModel } from '.././model/GameModel';
import wsTool from '../../../net/ws';
import CommConst from '../../../const/CommConst';
import { Ball } from '../Ball';
import { RigidBodyComponent } from 'cc';
import { RigidBody } from 'cc';
import { GameLayer1 as GameLayer, GameLayer1 } from './GameLayer1';
import { GameBaseModel } from '../GameBaseModel';
import { showTip, showTipBox } from '../../../tools/PopuUp';
import { EndLayer1 } from './EndLayer1';
import {  MAX_FORCE, MAX_FORCE_BOMB } from '../GameManage';
const { ccclass, property } = _decorator;

// 游戏主脚本
@ccclass('GameModel1')
export class GameModel1 extends GameBaseModel {

    protected onLoad(): void {
        super.onLoad();
    }

    protected onEnable(): void {
        super.onEnable();
    }
    
    start() {
       super.start();

       this.model.initModel1()
       this.model.setModel1Status(this.model.MODEL1_STATUS.STATUS_SYSTEM)
       setTimeout(() => {
        this.cueCtrl.showCue(false,this.cueCtrl.ballToCueAngle(this.getWhiteToOneAngle()))
       }, 100);
    }
    
    /**
     * 执行系统出杆
     */
    goSystemPlayball() {
        if  (!this.node) return;
        
        let model1_data = this.model.model1_data
        // 力度值 = 基础力度+等级力度+随机数力度
        let force = MAX_FORCE + (parseInt(model1_data.nums) / 15) * (MAX_FORCE_BOMB-MAX_FORCE-30) + 30*Math.random()

        UtilTool.printLog("力度值 force",force)
        setTimeout(()=>{
            if (this.node && this.model.model1Status == this.model.MODEL1_STATUS.STATUS_SYSTEM) {
                this.moveCue(this.getWhiteToOneAngle(), force)
            }
        },2000)
    }

    // 收到球进洞事件
    ballInRoleEvent(ballNumber : number) {
        if  (!this.node) return;
        UtilTool.printLog("GameModel1 start hit ball")
        if (this.model.model1Status == this.model.MODEL1_STATUS.STATUS_SYSTEM) {
            UtilTool.printLog("系统球进洞了，重新摆球")
            this.resetAllBallPos(false)

            showTip(App.language.getLanguageLab(74))
            this.goSystemPlayball()
        }
        else if(this.model.model1Status == this.model.MODEL1_STATUS.STATUS_PLAY) {
            if (ballNumber == parseInt(this.model.model1_data.nums)) {
                UtilTool.printLog("胜利了")
                App.Audio.playOneShot("ballDrop_clap") // 掌声
                this.model.model1Status = this.model.MODEL1_STATUS.STATUS_WIN 
                this.showEndLayer(1)
            }
            else {
                UtilTool.printLog("失败了")
                this.model.model1Status = this.model.MODEL1_STATUS.STATUS_FAIL 
                this.showEndLayer(2)
            }
        }
        else{

        }

    }

    // 收到开始击球事件
    startHitBallEvent(event) {
        UtilTool.printLog("GameModel1 start hit ball")
    }
    
    // 收到白球碰撞球事件
    whiteBallContactEvent(ballNumber : number) {
        this.isOpenBallStatus = false
        UtilTool.printLog("GameModel1 收到白球碰撞球事件")

    }

    // 收到摆球结束事件
    whiteBallPosEndEvent(whiteBallPos) {
        UtilTool.printLog("GameModel1 white ball pos end", whiteBallPos)

    }

    // 收到击球结束事件
    hitBallEndEvent() {
        if  (!this.node || this.model.model1_data==null) return;
        UtilTool.printLog("GameModel1 hit ball end")
        if (this.model.model1Status == this.model.MODEL1_STATUS.STATUS_SYSTEM) {
            UtilTool.printLog("系统出杆结束，轮到自己打球")
            this.model.model1Status = this.model.MODEL1_STATUS.STATUS_PLAY
            this.setHoleSwitch(true)
            this.setBallOrientation(true,parseInt(this.model.model1_data.nums))
            this.cueCtrl.showCue()
        }
        else if(this.model.model1Status == this.model.MODEL1_STATUS.STATUS_PLAY) {
            UtilTool.printLog("失败了")
            this.model.model1Status = this.model.MODEL1_STATUS.STATUS_FAIL
            this.showEndLayer(2)
        }
        else {
            
        }
    }

    update(deltaTime: number) {
        super.update(deltaTime)
    }
    /**
     * 展示结算界面
     * @param state [1]胜利 [2]失败
     */
    showEndLayer(state) {
        let that = this
        let stateType = state
        
        // 停止倒计时
        let gameLayer:GameLayer1 = this.getGameLayerCtrl(GameLayer1)
        gameLayer.stopDJS()

        // hitStatus 击球结果 2未进洞 1已进洞
        let hitStatus = stateType==1?1:2
        wsTool.send({"msgCode":CommConst.Req_HITBALL_RESOULT, "hitStatus":hitStatus, "id":gameLayer.getOrderId()})
        .then(res =>{
            if  (!this.node) return;
            if (res?.result == 0) {
                UtilTool.printLog("击球结果:",res)
                
                LoadMg.showLoad({type:1})
                resources.load('prefabs/game/layer1/GameEndLayer1',Prefab,(err,prefabs) =>{
                    let layerNode = instantiate(prefabs);
                    let popu = find('Canvas/viewNode')
                    popu.addChild(layerNode);
                    layerNode.getComponent(EndLayer1).showView(that, stateType)

                    LoadMg.removeLoad()
                })
            }
            else if (res?.result == 1000){ // 游戏不处于击球阶段
                showTipBox({
                    content:App.language.getLanguageLab(52),
                    func:()=>{
                        App.Model.gameView.backToHall()
                    }
                  })
            }
            else if (res?.result == 1001){ // 击球结果非法
                showTipBox({
                    content:App.language.getLanguageLab(53),
                    func:()=>{
                        App.Model.gameView.backToHall()
                    }
                  })
            }
            else {
                App.Model.gameView.backToHall()
            }
        })
    }
    
    
    /**
     * 游戏结束
     */
    public gameOver(data) {
    }

    onDestroy() {
        
    }

}

